package com.gene.service.impl;

import com.gene.mapper.TbContentMapper;
import com.gene.pojo.TbContent;
import com.gene.pojo.TbContentExample;
import com.gene.service.ITbContentService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;
@Service
public class TbContentServiceImpl implements ITbContentService {

    @Autowired
    private TbContentMapper contentMapper;

    /**
     * 查询类容 完成分页
     * @param pageNum
     * @param pageSize
     * @return
     */
    public PageInfo<TbContent> query(Integer pageNum,Integer pageSize, TbContent tbContent) {
        TbContentExample example=new TbContentExample();
        TbContentExample.Criteria criteria=example.createCriteria();
        if (pageNum==null ||pageNum==0){pageNum=1;}
        if (pageSize==null ||pageSize==3){pageNum=1;}
        if (tbContent.getId()!=null && tbContent.getId()!=0){
            criteria.andIdEqualTo(tbContent.getId());
        }
        if (tbContent.getCategoryId()!=null && tbContent.getCategoryId()!=0){
            criteria.andCategoryIdEqualTo(tbContent.getCategoryId());
        }
        if (tbContent.getTitle()!=null && !"".equals(tbContent.getTitle())){
            criteria.andTitleEqualTo(tbContent.getTitle());
        }
        if (tbContent.getSubTitle()!=null && !"".equals(tbContent.getSubTitle())){
            criteria.andSubTitleEqualTo(tbContent.getSubTitle());
        }
        if (tbContent.getTitleDesc()!=null &&  !"".equals(tbContent.getTitleDesc())){
            criteria.andTitleDescEqualTo(tbContent.getTitleDesc());
        }
        if (tbContent.getCreated()!=null &&  !"".equals(tbContent.getCreated())){
            criteria.andCreatedEqualTo(tbContent.getCreated());
        }
        if (tbContent.getUpdated()!=null &&  !"".equals(tbContent.getUpdated())){
            criteria.andUpdatedEqualTo(tbContent.getUpdated());
        }
        if (tbContent.getCreated()!=null &&  !"".equals(tbContent.getCreated())){
            example.setOrderByClause(tbContent.getCreated().toString());
        }
        PageHelper.startPage(pageNum,pageSize);
        List<TbContent> list = contentMapper.selectByExample(example);
        PageInfo<TbContent> pageInfo=new PageInfo<TbContent>(list);
        return pageInfo;
    }

    /**
     * 添加类容
     * @param content
     * @return
     */
    public boolean save(TbContent content) {
        content.setCreated(new Date());
        return contentMapper.insert(content)==1?true:false;
    }

    /**
     * 删除类容
     * @param id
     * @return
     */
    public boolean del(int id) {
        return contentMapper.deleteByPrimaryKey((long)id)==1?true:false;
    }

    /**
     * 修改类容
     * @param tbContent
     * @return
     */
    public boolean update(TbContent tbContent) {
        tbContent.setUpdated(new Date());
        return contentMapper.updateByPrimaryKeySelective(tbContent)==1?true:false;
    }
    /**
     * 根据id返回详情
     * @param id
     * @return
     */
    public TbContent content(Integer id){
        return  contentMapper.selectByPrimaryKey((long)id);
    }
}
